package com.nhii.appframe.mvp.presenter;

import android.app.Application;

import com.jess.arms.base.my.BaseEntity;
import com.jess.arms.base.my.BaseEntityEmpty;
import com.jess.arms.base.my.BaseLoadingSubscriber;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.nhii.appframe.mvp.contract.LoginContract;
import com.nhii.appframe.mvp.ui.activity.MainActivity;
import com.nhii.base.common.core.ProjectConstants;
import com.nhii.base.common.utils.RxUtil;
import com.nhii.base.common.utils.SPUtils;
import com.nhii.base.common.utils.ToastUtil;


import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import static com.nhii.base.common.utils.RxUtil.applySchedulers;

/**
 * ================================================
 * author : Alex
 * e-mail : xys9786@outlook.com
 * version: 1.0
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 05/05/2020 08:03
 * ================================================
 */
@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void sentLogin(String name, String pwd) {

        Observable<BaseEntity<BaseEntityEmpty>> observable = mModel.setLogin(name, pwd);

        Observable<BaseEntity<BaseEntityEmpty>> applySchedulers = RxUtil.applyLoading(mRootView, observable);

        applySchedulers.subscribe(new BaseLoadingSubscriber<BaseEntityEmpty>(mRootView) {
            @Override
            protected void onRequestSuccess(BaseEntityEmpty data) {
                mRootView.showMessage("登录成功");
            }

            @Override
            protected void onRequestError(String data) {
                mRootView.showMessage("登录失败");
                ToastUtil.show(data);
            }
        });

    }

}
